<template>
    <view class="shopitem item" v-if="objInfo && objInfo.id">
        <view class="store flexb">
            <view class="store_name flexb">
                <image class="icon" :src="imgpath + 'store_icon.png?v=' + imgVersion" mode="aspectFit" > </image>
                <view class="txt">{{ objInfo.company }}</view>
            </view>
            <view class="order_state">{{ stateTxt(objInfo) }}</view>
        </view>
        <view class="order-content" @click="toLink('detail')">
            <view class="prosbox">
                <view class="pros_detail flexb" v-if="objInfo.product">
                    <image class="pro-img loadingImg" :src="objInfo.product.litpic || defaultPro" mode="aspectFill"  @error="objInfo.product.litpic = defaultPro "></image>
                    <view class="pro-info ">
                        <view class="pro_detail flexb">
                            <view class="flex_l">
                                <view class="pro-tit">{{ objInfo.product.title }}</view>
                                
                                <view class="pro-speci" v-if="objInfo.product.specation">{{ specationText(objInfo.product.specation) }}</view>
                                <view class="pro-speci" v-else-if="objInfo.tuantype == '0' && objInfo.orderstate == '1'">有效期至：{{ timeTrans(objInfo) }}</view>
                            </view>
                            <view class="pro-price">
                                <view class="price">{{globalData.appConfig && globalData.appConfig.currency && globalData.appConfig.currency.symbol || '￥'}}{{ objInfo.orderprice }}</view>
                                <view class="num">x1</view>
                                <view v-if="objInfo.orderstate == '7'" class="tkcg">退款成功</view>
                            </view>
                        </view>
                        
                    </view>
                </view>
                <!-- <view class="proimgList flexb" v-else>
                    <image class="pro-img" v-for="(item,ind) in objInfo.product" :key="ind" :src="item.litpic || defaultPro" mode="aspectFill"  @error="item.litpic = defaultPro "></image>
                </view> -->
            </view>
            <view class="order-count flexb">
                <view class="flex_l">共<text class="txt">1</text>件商品</view>
                <view class="flex_r flexb">
                    <text class="txt">{{objInfo.pinid != 0 ? '拼团价' : '实付'}}</text>
                    <view class="tprice">
                        <text class="symbol">{{globalData.appConfig && globalData.appConfig.currency && globalData.appConfig.currency.symbol || '￥'}}</text>
                        <text class="b">{{ objInfo.orderprice.split('.')[0] }}</text>
                        <text class="smNumber" v-if=" objInfo.orderprice.includes('.')">.{{  objInfo.orderprice.split('.')[1] }}</text>
                    </view>
                </view>
            </view>
            <view class="btns_group flexb">
                <block v-if="objInfo.orderstate == 0">
                    <view class="btn_cancel  btn" @click.stop="toLink('cancel')">取消订单</view>
                    <view class="btn_cancel  btn" @click.stop="toLink('pay_other')" v-if="tuanConfig && tuanConfig.peerpayState == 1">取消订单</view>
                    <view class="btn_pay bgBtn  btn"  @click.stop="toLink('pay')">去付款</view>
                </block>
                <block v-else-if="objInfo.orderstate == 2">
                    <view class="btn_tuikuan  btn" v-if="objInfo.product.is_tuikuan == '0' && objInfo.paydate">申请退款</view>
                    <view class="btn_tuikuan  btn" v-else>删除订单</view>
                </block>
                <block v-else-if="objInfo.orderstate == 3">
                    <view class="btn_buy  btn" v-if="objInfo.common == '1'">再次购买</view>
                    <view class="btn_common borderBtn btn" @click.stop="toLink('comment')" v-else>评价</view>
                </block>
                <view class="btn_toDetail" v-else-if="objInfo.orderstate == 4">查看详情</view>
                <!-- btn_tkStep -->
                <block v-else-if="objInfo.orderstate == 6 && objInfo.retState == 0 ">
                    <view class="btn_wuliu  btn">查看物流</view>
                    <view class="btn_shou borderBtn btn">确认收货</view>
                </block>
              
                <block v-else-if="objInfo.orderstate == 1">
                    <!-- tuikuan 是否支持退款 -->
                    <block v-if="objInfo.tuantype != 2">
                        <!-- <view v-if="objInfo.pinid != 0 && objInfo.pinstate == 0" @click.stop="toLink('pindetail')" class="btn_invite bgBtn btn">邀请好友拼团</view> -->
                        <button  v-if="objInfo.pinid != 0 && objInfo.pinstate == 0" open-type="share" @click.stop="toLink('share')" :data-orderInd="ind" style="margin-right: 0;" class="btn_invite bgBtn btn">邀好友拼团</button>
                        <template v-else>
                            <!-- <view class="btn_tuikuan  btn" v-if="objInfo.product.is_tuikuan == '0'">申请退款</view>     -->
                            <view class="btn_showID borderBtn btn" v-if="objInfo.cardnumUrl" @click.stop="toLink('quan')">查看券码</view>
                        </template>
                    </block>
                    <template v-else>
                        <view class="btn_tuikuan  btn" v-if="objInfo.product.is_tuikuan == '0'">申请退款</view>
                        <view class="btn_toDetail  btn" v-else>查看详情</view>
                    </template>
                    
                </block>

                <block  v-else-if="objInfo.orderstate == 11 || (objInfo.orderstate == 6 && objInfo.reState == 1 && objInfo.tuantype  == 0 && objInfo.shopquan != 0)">
                    <view class="btn_toDetail  btn">查看详情</view>
                </block>
                
                <!-- <view v-else-if="objInfo.orderstate == '7'" class="btn_tkStep  btn">查看退款</view>     -->
            </view>
        </view>
    </view>
</template>



<script>
    const app = getApp();
    export default {
        props:{
            objInfo:{
                type:Object,
                default:{
                    
                }
            },
            ind:{
                type:Number,
                default:0
            },
            tuanConfig:{
                type:Object,
                default:{}
            }
        },
        data(){
            return {
                defaultPro:app.globalData.masterDomain + '/static/images/good_default.png',
                globalData:app.globalData,
                platForm:app.globalData.platForm,
                imgpath:app.globalData.imgPath + '/member/order/',
                masterDomain:app.globalData.masterDomain,
                imgVersion:app.globalData.imgVersion,
            }
        },
        computed:{
            stateTxt(){
                return function(order){
                    const that = this;
                    let txt = ''
                    switch(order.orderstate){
                        case '0':
                            txt = '未付款'
                            break;
                        case '1':
                            txt = '待发货'
                            if(order.tuantype != 2){
                                if(order.pinid != '0' && order.pinstate == '0'){
                                    txt = '拼团中';
                                }else{
                                    txt = '待使用'
                                }
                            }else{
                                if(order.pinid != '0' && order.pinstate == '0'){
                                    txt = '拼团中';
                                }else{
                                    txt = '待发货'
                                }
                            }
                            
                            break;
                        case '2':
                            txt = order.paydate > 0 ? '已过期' : '未付款，已过期'
                            break;
                        case '3':
                            txt = '交易成功'
                            break;
                        case '4':
                            txt = '退款中'
                            break;
                        case '6':
                            txt = order.retState == 1 ? '退款中' : '待收货'
                            break;
                        case '7':
                            txt = '交易关闭'; //退款成功
                            break;
                        
                    }
                    return txt
                }
            },

            // 时间戳转换
            timeTrans(){
                return function(order){
                    let timestr = order.product.enddate
                    const dateFormatter = app.dateFormatter(timestr);
                    const year = dateFormatter.year;
                    const month = dateFormatter.month;
                    const day = dateFormatter.day;
                    const hour = dateFormatter.hour;
                    const minute = dateFormatter.minute;
                    // const second = dateFormatter.second;
                    
                    let today_dateFormatter = app.dateFormatter(parseInt(new Date().valueOf() / 1000));
                    const today_year = today_dateFormatter.year;
                    // const today_month = today_dateFormatter.month;
                    let showStr = '';
                    showStr = year + '.' + month + '.' + day + ' ' + hour + ':' + minute;
                    return showStr
                }
            },

            specationText(){
                return function(specation){
                    let speArr = specation.split(' ');
                    let speTxtArr = speArr.map(item => {
                        let pec = ''
                        let arr = item.split('：');
                        if(arr.length > 1){
                            pec = arr[1]
                        }
                        return pec;
                    })
                    return speTxtArr.join(';')
                }
            }
        },
        mounted(){
            // console.log(this.objInfo)
        },
        methods:{
            // 验证是否过期
            checkExpire(expireddate){
                var Ret = new RegExp("\\.","g");//替换所有的点为-
                // let expireddate = that.objInfo.expireddate || ''
                expireddate = expireddate.replace(Ret,"-")
                var nowDateArr = (new Date()).getTime();
                var expDateArr = (new Date(expireddate)).getTime();
                return expDateArr > nowDateArr
            },

            toLink(type){
                const that = this;
                if(type == 'detail' || type == 'comment'){
                    let url = `${app.globalData.appConfig.cfg_user_index}/orderdetail-tuan-${that.objInfo.id}.html${ type == 'comment' ? '?rate=1' : ''}`
                    app.linkTo(url,false,false)
                }else if(type == 'pay_other'){
                    let url = that.globalData.appConfig.cfg_user_index + '/daipay.html?module=tuan&ordernum=' + that.objInfo.ordernum;
                    app.linkTo(url,false,false)
                }else if(type == 'pindetail'){
                    let url = `${app.globalData.masterDomain}/tuan/dindan-${that.objInfo.pinid}.html`
                    app.linkTo(url,false,false)
                }else if(type == 'quan'){
                    uni.$emit('showwuliu',{objInfo:this.objInfo,ind:this.ind})
                }else if(type == 'pay'){
                    // 支付订单
                    uni.$emit('payorder',{objInfo:this.objInfo,ind:this.ind})
                }else if(type == 'cancel'){
                    // 取消订单
                    uni.$emit('cancelorder',{objInfo:this.objInfo,ind:this.ind})
                }else if(type == 'share'){
                    if(app.globalData.platForm == 'web'){
                        uni.$emit('share',{objInfo:this.objInfo,ind:this.ind})
                    }
                }
            }
        }
    }
</script>

<style scoped>
    @import './orderlist_tuan.css'
</style>